<template>
  <div class="points-container">
    <PageHeader title="积分明细" />

    <!-- 积分信息卡片 -->
    <div class="points-card">
      <div class="points-total">{{ userStore.userInfo?.points || 0 }}</div>
      <div class="points-label">当前积分</div>
    </div>

    <!-- 积分明细列表 -->
    <div class="points-list">
      <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div v-if="pointsList.length === 0 && !loading" class="empty-text">暂无积分记录</div>
        <div v-for="(item, index) in pointsList" :key="index" class="points-item">
          <div class="points-item-left">
            <div class="points-item-desc">{{ item.description }}</div>
            <div class="points-item-time">{{ formatTime(item.createdAt) }}</div>
          </div>
          <div
            class="points-item-amount"
            :class="item.type === '收入' ? 'income' : 'expense'"
          >{{ item.type === '收入' ? '+' : '-' }}{{ item.amount }}</div>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { getUserPoints } from '@/api/user'
import PageHeader from '@/components/PageHeader.vue'

const userStore = useUserStore()
const pointsList = ref<any[]>([])
const loading = ref(false)
const finished = ref(false)
const page = ref(1)
const limit = ref(10)

// 加载积分明细
const onLoad = async () => {
  if (finished.value) return

  loading.value = true
  try {
    const res = await getUserPoints({
      page: page.value,
      limit: limit.value,
      userId: userStore.userInfo?.id,
    })

    if (res.code === 200 && res.data) {
      const newList = res.data.list || []
      pointsList.value.push(...newList)

      // 更新页码
      page.value++

      // 判断是否加载完毕
      if (newList.length < limit.value) {
        finished.value = true
      }
    } else {
      finished.value = true
    }
  } catch (error) {
    console.error('获取积分明细失败:', error)
    finished.value = true
  } finally {
    loading.value = false
  }
}

// 格式化时间
const formatTime = (time: string) => {
  if (!time) return ''
  const date = new Date(time)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(
    2,
    '0'
  )}-${String(date.getDate()).padStart(2, '0')} ${String(
    date.getHours()
  ).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
}

onMounted(() => {
  // 初始化加载
  if (userStore.isLoggedIn) {
    // 确保用户已登录
    onLoad()
  }
})
</script>

<style scoped lang="scss">
.points-container {
  min-height: 100vh;
  background-color: $background-color;
  padding-bottom: 50px;
}

.points-card {
  margin: $padding-md;
  padding: $padding-lg;
  background-color: $primary-color;
  border-radius: $border-radius-lg;
  color: $white;
  text-align: center;

  .points-total {
    font-size: 36px;
    font-weight: bold;
    margin-bottom: $padding-xs;
  }

  .points-label {
    font-size: $font-size-md;
    opacity: 0.8;
  }
}

.points-list {
  background-color: $white;
  margin-top: $padding-xs;
}

.empty-text {
  text-align: center;
  padding: $padding-xl 0;
  color: $text-color-light;
}

.points-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: $padding-md;
  border-bottom: 1px solid #f5f5f5;

  &:last-child {
    border-bottom: none;
  }

  .points-item-left {
    flex: 1;
    margin-right: $padding-md;

    .points-item-desc {
      color: $text-color;
      font-size: $font-size-md;
      margin-bottom: 4px;
    }

    .points-item-time {
      color: $text-color-light;
      font-size: $font-size-sm;
    }
  }

  .points-item-amount {
    font-size: $font-size-lg;
    font-weight: 500;

    &.income {
      color: $success-color;
    }

    &.expense {
      color: $danger-color;
    }
  }
}
</style> 